﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcWeb.Models;
using SailHero.Soft.FrameWork.IDomainRepositories.Contracts;
using SailHero.Soft.FrameWork.Models;

namespace MvcWeb.Controllers
{
    [Export(typeof(IController)), ExportMetadata("Name", "Model")]
    public class ModelController : BaseController
    {
        //
        // GET: /Model/
        /// <summary>
        /// 数据操作接口
        /// </summary>
        [Import]
        public IModelRepository modModel;


        [Authorize]
        public ActionResult Index(string page)
        {
            var list = modModel.GetAllList();
            list = list.OrderBy(m => m.ORDERNUM);
            int size = 10;
            if (page != null)
            {
                ViewData["page"] = PagerHelper.Pager(Request.RawUrl.Substring(0, Request.RawUrl.IndexOf("?")), Convert.ToInt32(page), list.Count(), size, 4);
                ViewBag.Page = PagerHelper.Pager(Request.RawUrl.Substring(0, Request.RawUrl.IndexOf("?")), Convert.ToInt32(page), list.Count(), size, 4);
                list = list.Skip((Convert.ToInt32(Request["page"]) - 1) * size).Take(size);
            }
            else
            {
                ViewData["page"] = PagerHelper.Pager(Request.RawUrl, 1, list.Count(), size, 4);
                ViewBag.Page = PagerHelper.Pager(Request.RawUrl, 1, list.Count(), size, 4);
                list = list.Skip((1 - 1) * size).Take(size);
            }
            return View(list.AsEnumerable());
        }

        [Authorize]
        public ActionResult Create()
        {
            ViewData["modList"] = new SelectList(modModel.GetAllList(), "ModelID", "ModelCName");
            return View();
        }

        [Authorize]
        public string Create2()
        {
            try
            {
                SYS_MODEL model = new SYS_MODEL
                {
                    MODELID = DateTime.Now.Ticks.ToString(),
                    MODELCNAME = HttpUtility.UrlDecode(Request.Form["modelName"]),
                    PARENTID = Request.Form["modelPartent"],
                    URL = Request.Form["modURL"],
                    ORDERNUM = Request.Form["modOrderNum"],
                    ISSHOW = Request.Form["isShow"] == "1" ? "1" : "0",
                    CREATOR = LoginUser.ACCOUNT,
                    CREATETIME = DateTime.Now
                };
                modModel.Add(model);
                modModel.UnitOfWork.SaveChanges();
                return "success";

            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        [Authorize]
        public ActionResult Edit(string id)
        {
            SYS_MODEL model = modModel.Get(id);
            ViewData["modList"] = new SelectList(modModel.GetAllList(), "ModelID", "ModelCName", model.PARENTID);
            return View(model);
        }

        [Authorize]
        public string Edit2(string id)
        {
            try
            {
                SYS_MODEL model = modModel.Get(id);
                model.MODELCNAME = HttpUtility.UrlDecode(Request.Form["modelName"]);
                model.PARENTID = Request.Form["modelPartent"];
                model.URL = Request.Form["modURL"];
                model.ORDERNUM = Request.Form["modOrderNum"];
                model.ISSHOW = Request.Form["isShow"] == "1" ? "1" : "0";
                model.UPDATEUSER = LoginUser.ACCOUNT;
                model.UPDATETIME = DateTime.Now;
                modModel.UnitOfWork.SaveChanges();

                return "success";

            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        [Authorize]
        public string Delete(string id)
        {
            var list = (modModel.GetList(model => model.PARENTID == id)).ToList();
            if (list.Count > 0)
            {
                return "该模块下面有子模块，请先删除子模块！";
            }
            else
            {
                try
                {
                    var model = modModel.FirstOrDefault(m => m.MODELID == id);
                    modModel.Delete(model);
                    modModel.UnitOfWork.SaveChanges();
                    return "success";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }

        }
    }
}
